﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Meta.Numerics.Statistics;
using System.Collections;
using StatisticsApplication.WindowsForm;
using StatisticsApplication.MyClass;
using Meta.Numerics.Functions;

namespace StatisticsApplication
{
    public partial class Main : Form
    {
        DateTime beginTime;
        DateTime endTime;
        public Main()
        {
            InitializeComponent();
            btCancle.Enabled = false;
        }

        private void studentT_Click(object sender, EventArgs e)
        {

        }

        private void btBeginCreat_Click(object sender, EventArgs e)
        {
            /*BinomialCoefficient();*/
           //long k= AdvancedIntegerMath.BinomialCoefficient(8, 1) * AdvancedIntegerMath.BinomialCoefficient(7, 3);
          // MessageBox.Show(k.ToString());
            beginTime = DateTime.Now;
            btBeginCreat.Enabled = false;
            btCancle.Enabled = true;
            listBox1.Items.Clear();
            backgroundWorker1.RunWorkerAsync();
        }


        private int  MyTimes;
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            
            if (!tbControl1.MyStatus)
            {
                MessageBox.Show(null, "数据没有准备好！", "温馨提示：", MessageBoxButtons.OK);
                return;
            }

            string Number = tbControl1.Words;

            int allCount = 0;
            for (int i = 0; i < tbControl1.GroupNum;i++ )
            {
                allCount += tbControl1.EachNum[i];
            }
            long allTimes = 1;
            int rember=0;
            for (int i = 0; i < tbControl1.GroupNum; i++)
            {
                //allCount += tbControl1.EachNum[i];
               allTimes *= AdvancedIntegerMath.BinomialCoefficient(allCount - rember, tbControl1.EachNum[i]);
               rember += tbControl1.EachNum[i];
            }

            MyTimes = (int)allTimes;
            string[] kk = Number.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            CombinationClass cbclass = new CombinationClass(backgroundWorker1);
            string tt=string.Empty;
            switch (tbControl1.GroupNum)
            {
                case 2:
                    tt = cbclass.Twosort(kk, tbControl1.EachNum[0], listBox1);
                    break;
                case 3:
                    tt = cbclass.Threesort(kk, tbControl1.EachNum[0], tbControl1.EachNum[1], listBox1);
                    break;
                case 4:
                    tt = cbclass.Foursort(kk, tbControl1.EachNum[0], tbControl1.EachNum[1], tbControl1.EachNum[2], listBox1);
                    break;
                case 5:
                    tt = cbclass.Fivesort(kk, tbControl1.EachNum[0], tbControl1.EachNum[1], tbControl1.EachNum[2], tbControl1.EachNum[3], listBox1);
                    break;
                case 6:
                    tt = cbclass.Sixsort(kk, tbControl1.EachNum[0], tbControl1.EachNum[1], tbControl1.EachNum[2], tbControl1.EachNum[3], tbControl1.EachNum[4], listBox1);
                 
                    break;
                case 7:
                    tt = cbclass.Sevensort(kk, tbControl1.EachNum[0], tbControl1.EachNum[1], tbControl1.EachNum[2], tbControl1.EachNum[3], tbControl1.EachNum[4], tbControl1.EachNum[5],listBox1);
                 
                    break;
                case 8:
                    tt = cbclass.Eightsort(kk, tbControl1.EachNum[0], tbControl1.EachNum[1], tbControl1.EachNum[2], tbControl1.EachNum[3], tbControl1.EachNum[4], tbControl1.EachNum[5],tbControl1.EachNum[6], listBox1);
                 
                    break;
                case 9:
                    tt = cbclass.Ninesort(kk, tbControl1.EachNum[0], tbControl1.EachNum[1], tbControl1.EachNum[2], tbControl1.EachNum[3], tbControl1.EachNum[4], tbControl1.EachNum[5], tbControl1.EachNum[6],tbControl1.EachNum[7], listBox1);
                 
                    break;
                case 10:
                    tt = cbclass.Tensort(kk, tbControl1.EachNum[0], tbControl1.EachNum[1], tbControl1.EachNum[2], tbControl1.EachNum[3], tbControl1.EachNum[4], tbControl1.EachNum[5], tbControl1.EachNum[6], tbControl1.EachNum[7],tbControl1.EachNum[8], listBox1);
                 
                    break;
            }
            if (tt.IndexOf("cancle")>0)
            {
                e.Cancel = true;
            }
            else
            {
                e.Result = tt;
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            statusStrip1.Refresh();
            toolStripStatusLabel1.Text = e.ProgressPercentage.ToString();
            this.toolStripProgressBar1.Value = (int)e.ProgressPercentage * 100 / MyTimes;
            this.toolStripStatusLabel4.Text = (int)e.ProgressPercentage * 100 / MyTimes + "%";
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            endTime = DateTime.Now;
            if (e.Cancelled)
            {
                this.toolStripStatusLabel4.Text = "取消生成！";
            }
            else
            {
                this.toolStripStatusLabel4.Text = "生成完毕"+e.Result;
                this.toolStripProgressBar1.Value = 100;
            }
            btCancle.Enabled = false;
            btBeginCreat.Enabled = true;
            toolStripStatusLabel4.Text += "耗费:" + (endTime - beginTime).TotalMilliseconds.ToString() + "ms";
        }

        private void btCancle_Click(object sender, EventArgs e)
        {
            btBeginCreat.Enabled = true;
            btCancle.Enabled = false;
            backgroundWorker1.CancelAsync();
        }
    }
}
